package controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import util.Util;
import bean.Fund;
import bean.FundTransaction;
import bean.LoggedUser;
import bean.Model;
import bean.Position;
import bean.UserType;
import businessBean.CustomerFundPosition;
import dao.FundDAO;
import dao.PositionDAO;

/*
 * 
 * Created By Sahil Thakkar for Sell Fund  Usecase
 */

public class ViewPortfolioAction extends Action {
	
	private PositionDAO positionDAO;
	private FundDAO fundDAO;
	public ViewPortfolioAction(Model model) {
		positionDAO = model.getPositionDAO();
		fundDAO = model.getFundDAO();
	}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "viewPortfolio.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		// TODO Auto-generated method stub
		String fundStr = null;
		Fund fund = null;
		List<FundTransaction> fundTransactionList = null;
		try {
			
			HttpSession session     = request.getSession(false);
			LoggedUser user = (LoggedUser) session.getAttribute("user");
			
			List<Position> position = positionDAO.getPositionByCusId(""+user.getId());
			List<Fund> allFunds = fundDAO.getAllFund();
			
			List<CustomerFundPosition> lstCustomerFundPosition = new ArrayList<CustomerFundPosition>();
			Iterator<Position> positionIter = position.iterator();
			
			while(positionIter.hasNext())
			{
				Position p = positionIter.next(); 
				CustomerFundPosition customerFundPosition = new CustomerFundPosition();
				customerFundPosition.setFundName(p.getFund().getName());
				customerFundPosition.setCustomerId(user.getId());
				customerFundPosition.setFundPosition(Util.showShares(p.getShares()));
				customerFundPosition.setFundSymbol(p.getFund().getSymbol());
				
				lstCustomerFundPosition.add(customerFundPosition);
			
				System.out.println("Fund Name" + customerFundPosition.getFundName());
				System.out.println("Customer Id" + customerFundPosition.getCustomerId());
				System.out.println("Fund Position" + customerFundPosition.getFundPosition());
				System.out.println("Fund Symbol" + customerFundPosition.getFundSymbol());
				System.out.println("----------------------------------------------");
			}
		    request.setAttribute("lstCustomerFundPosition", lstCustomerFundPosition);
			
		    
		    
		    
			return "viewPortfolio.jsp";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error.jsp";
		}
	}

	 @Override
		public UserType getType() {
		 return UserType.Customer;
	 }
}